<?php
namespace Index\Controller;
use Index\Common\commonController;
header("Content-type: text/html; charset=utf-8");
class SettingController extends CommonController {
    //基本资料
    public function BasingAction(){ 
        if(IS_POST){
            // var_dump(I('post.'));
            // exit();
            $userid = $_SESSION['id'];
            $sex = I('post.sex');
            $birthday_y = I('post.birthday_y');
            $birthday_m = I('post.birthday_m');
            $birthday_d = I('post.birthday_d');
            $selectp = I('post.selectp');
            $selectc = I('post.selectc');
            $jobs = I('post.jobs');
            $signature = I('post.signature');
            $url_token = I('post.url_token');
            $default_timezone = I('post.default_timezone');
            $qq = I('post.qq');
            $mobile = I('post.mobile');
            $common_email = I('post.common_email');
            $homepage = I('post.homepage');
            $birthday = "$birthday_y-$birthday_m-$birthday_d";

            // $sql = "insert into user(sex,birthday,province,city,jobs,introduce,qq,phone,mail,site) values('$sex','$birthday','$selectp','$selectc','$job_id','$signature','$qq','$mobile','$common_email','$homepage')";
            $sql = "update user set sex='$sex',birthday='$birthday',province='$selectp',city='$selectc',jobs='$jobs',introduce='$signature',qq='$qq',phone='$mobile',mail='$common_email',site='$homepage' where id = $userid";
            $data_m = M();
            $res = $data_m->execute($sql);
            if ($res ) {
                 $this->success("数据操作成功",U("index/setting/basing"),3); 
            }else{
                 $this->error("数据操作失败");
            }

        }else{
            $uid=$_SESSION['id'];
            //查询数据库中的职位
            $sql="select * from position_t";
            $data_m=M();
            $position_res=$data_m->query($sql);
            
            //根据用户ID查找出相关的资料
            $user_sql="select * from user where id='$uid'";
            $user_res=$data_m->query($user_sql);
            $user_res=current($user_res);
            // var_dump($user_res);
            // exit();
            $this->assign('user_res',$user_res);//分配用户信息到模板上
            $this->assign('position_res',$position_res);
            $this->display();  
        }
    	
    }

    //上传头像信息
     public function faceAction(){
        if(!empty($_FILES)){          
            $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/face/',
            'savePath'   =>    '',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类

        $info   =   $upload->upload();

        if(!$info) {// 上传错误提示错误信息    
             $this->error($upload->getError());
        }else{
        // 上传成功 获取上传文件信息    
            $info=current($info);
            $face = 'Uploads/face/'.$info['savepath'].$info['savename'];
            $uid = $_SESSION['id'];
            $sql = "update user set head_img='{$face}' where id='{$uid}'";
            $model = M();
            $info = $model->execute($sql);
            $_SESSION['face'] = $face;
            // var_dump($face); 
            // exit();
        }

        }else{
           $this->display(); 
       }
    }

    //密码修改
     public function securityAction(){     
        // var_dump($_SESSION['id']);
        $useid = $_SESSION['id'];
        $old_password = I('post.old_password');
        $password = I('post.password');
        $re_password = I('post.re_password');
        $sql = "select * from user where id='{$useid}'";
        $data_m = M();
        $res = $data_m->query($sql);
        $res = current($res);
        if(IS_POST){
            if($res['password']!=md5($old_password)){
                $this->error("老密码输入不正确",U('index/setting/security'),2);
            }else{
               if($password!=$re_password){
                    $this->error("新密码不一致",U('index/setting/security'),2);//这样居然有问题
                // $this->error("新密码不一致");
               }else{
                 $password = md5($password);
                 $sql = "update user set password = '{$password}'";
                 $data_m = M();
                 $res = $data_m ->execute($sql);
                 // var_dump($res);
                 // exit();
                 if($res){
                    session_unset();
                    session_destroy();
                    $this->error("新密码更改成功,请重新登录",U('index/login/login'),2);
                 }
                 
               }
               
             }
        }else{
            $this->display();
        }  	
    }


    //认证中心
     public function verifyAction(){
         $uid = $_SESSION['id'];
        if(IS_POST){
            $type = I('POST.type');
            $name = I('POST.name');
            $id_code = I('POST.id_code');
            $contact = I('POST.contact');
            $reason = I('POST.reason');
            $submit = I('POST.submit');
            $_SESSION['verified'] = 0;      
            // var_dump($res);
            // exit();
            if(($type=='personal')||($type=='enterprise')){

             $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/info/',
            'savePath'   =>    '',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
            );
        $upload = new \Think\Upload($config);// 实例化上传类

        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息    
             $this->error($upload->getError());
        }else{
        // 上传成功 获取上传文件信息    
            $info=current($info);
            $verify_file = 'Uploads/info/'.$info['savepath'].$info['savename'];
            // $_SESSION['verify_file'] = $verify_file;
            // var_dump($face); 
            // exit();
            $sql = "insert into verify(type,truename,idcard,phone,instruction,uid,verify_file) values('$type','$name','$id_code','$contact','$reason','$uid','$verify_file')";
                $data_m = M();
                $res = $data_m->execute($sql);
                if($res){
                    $this->success('认证完成成功','',3);
                }else{
                    $this->error('认证失败');
                }
        }
                
            }
                
        }else{
            //进来第一步先查看该登录用户在认证表中是否存在
            $sql = "select * from verify where uid = '{$uid}'";
            $data_m = M(); //实例化M对象
            $res = $data_m ->query($sql);
            $res = current($res);
            // var_dump($res);
            $this->assign('res',$res); //分配变量到模板
            $this->display();
        }
    	
    }

    //处理添加教育信息传过来的数据
    public function add_eduAction(){
            if(IS_POST){

                $User = M("education"); // 实例化User对象
                $data['school']= I('POST.school_name');
                $data['year']= I('POST.education_years');
                $data['profession']= I('POST.departments');
                $data['uid']= $_SESSION['id'];
                $id = $User->add($data); //用add方法而不用execute是因为会返回插入的id值。
                // var_dump($id);
                // exit();
                if ($id) {
                    //用一个数组存储需要返回给JS用的值，先去看看需要接受何值
                    $arr = array('errno' => 1,'err' =>'对不起，插入操作失败','rsm' => array('id' => $id));
                    $json = json_encode($arr);
                    echo $json;
                }

            }
    }

    /**
     * 中文分词  
         * @params string $title 需要分词的语句 
         * @params int $num  分词个数，默认不用填写
     **/
    // private function get_tagsAction($title,$num=null){        
    //     $pscws = new \Org\Util\Pscws('utf8');
    //     $pscws->set_dict(CONF_PATH . 'dict.utf8.xdb');
    //     $pscws->set_rule(CONF_PATH . 'rules.utf8.ini');
    //     $pscws->set_ignore(true);
    //     $pscws->send_text($title);
    //     $words = $pscws->get_tops($num);
    //     $pscws->close();
    //     $tags = array();
    //     foreach ($words as $val) {
    //         $tags[] = $val['word'];
    //     }
    //     return implode(',', $tags);
    // }
    //   /**
    //  * 商品搜索结果页
    //  **/
    // public function searchAction(){
    //     $rzt=$this->get_tags("新款 牛漆皮小尖头直跟高跟单鞋910033 灰羊猄(7.31发货) 39");
    //     print_r($rzt);
    // }





}